package com.example.service;

import com.example.model.Review;
import com.example.repository.ReviewRepository;
import org.springframework.stereotype.Service;
import org.springframework.security.core.Authentication;

import java.io.IOException;
import java.util.List;

@Service
public class ReviewService {

    private final ReviewRepository reviewRepository;

    public ReviewService(ReviewRepository reviewRepository) {
        this.reviewRepository = reviewRepository;
    }

    public Review createReview(Review review, String productId, Authentication authentication) throws IOException {
        String username = authentication.getName();
        review.setProductId(productId);
        review.setUserId(username);
        review.setUserName(username); // Also set the username for display

        reviewRepository.save(review);
        return review;
    }

    public List<Review> getReviewsByProductId(String productId) throws IOException {
        return reviewRepository.findByProductId(productId);
    }

    // Add other business logic methods here
} 